package com.mi.million.module.system.controller;

import com.alibaba.fastjson.JSONArray;
import com.mi.million.module.system.service.RoleService;
import com.mi.million.module.system.service.SystemService;
import com.mi.million.mybatis.entity.SysMenu;
import com.mi.million.mybatis.entity.SysResource;
import com.mi.million.mybatis.entity.SysRole;
import com.mi.rule.resource.ResC;
import com.mi.rule.resource.ResM;
import com.mi.rule.util.project.SRS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@ResC(value = "角色管理", parent = "系统配置")
@RestController
@RequestMapping("/system/role")
public class RoleController {
    @Autowired
    RoleService roleService;
    @Autowired
    SystemService systemService;

    @ResM("添加数据")
    @RequestMapping({"/insert"})
    public SRS insert(@RequestBody SysRole role) {
        return roleService.insertSysRole(role);
    }

    @ResM("删除数据")
    @RequestMapping({"/delete"})
    public SRS delete(@RequestBody Object[] ids) {
        return roleService.deleteSysRole(ids);
    }

    @ResM("修改数据")
    @RequestMapping({"/update"})
    public SRS update(@RequestBody SRS param) {
        return roleService.updateSysRole(param);
    }

    @ResM("列表查询")
    @RequestMapping({"/list"})
    public SRS list(@RequestBody SRS param) {
        return roleService.listSysRole(param);
    }

    @ResM("分页查询")
    @RequestMapping("/page")
    public SRS page(@RequestBody SRS param) {
        return roleService.pageSysRole(param);
    }

    @ResM("详情查询")
    @RequestMapping("/info")
    public SRS one(@RequestBody SRS param) {
        return roleService.infoSysRole(param);
    }

    @ResM("角色菜单")
    @RequestMapping("/roleMenu")
    public SRS roleMenu(@RequestBody SRS param) {
        List<String> menuCheckIds = new ArrayList<>();
        List<SysMenu> hasMenus = new ArrayList<>();
        List<SysMenu> menus = new ArrayList<>();

        String id = param.getString("id");
        JSONArray ids = param.getJSONArray("ids");
        menus = systemService.menus();


        if (id != null) {
            hasMenus = roleService.roleMenuList(id);
        }
        if (ids != null) {
            hasMenus = roleService.roleMenuList(ids);
        }

        if (hasMenus.size() > 0) {
            menuCheckIds = hasMenus.stream().filter(it -> !it.getPid().equals("0")).map(SysMenu::getId).collect(Collectors.toList());
        }


        return SRS.bySuccess().set("menus", menus).set("hasMenus", hasMenus).set("menuCheckIds", menuCheckIds);
    }

    @ResM("角色资源")
    @RequestMapping("/roleResource")
    public SRS roleResource(@RequestBody SRS param) {
        List<String> resourceCheckIds = new ArrayList<>();
        List<SysResource> resources = new ArrayList<>();
        List<SysResource> hasResources = new ArrayList<>();

        String id = param.getString("id");
        JSONArray ids = param.getJSONArray("ids");
        resources = systemService.resources();

        if (id != null) {
            hasResources = roleService.roleResourceList(id);
        }

        if (ids != null) {
            hasResources = roleService.roleResourceList(ids);
        }

        if (hasResources.size() > 0) {
            resourceCheckIds = hasResources.stream().filter(it -> !it.getPid().equals("0")).map(SysResource::getId).collect(Collectors.toList());
        }

        return SRS.bySuccess().set("resources", resources).set("hasResources", hasResources).set("resourceCheckIds", resourceCheckIds);
    }

    //    @ResM("获取全部菜单和资源", author = "王永吉")
    //    @RequestMapping(value = "/allMenuResource")
    //    public SRS allMenuResource(@RequestBody SRS param) throws Exception {
    //        return systemService.allMenuResource(param);
    //    }
    //
    //
    //    @ResM("查询全部角色", author = "王永吉")
    //    @RequestMapping(value = "/allRole")
    //    public SRS allRole(@RequestBody SRS param) throws Exception {
    //        return systemService.allRole(param);
    //    }


}
